package com.example.forum.controller;

import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import com.example.forum.utils.StringUtil;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author YJ
 * @Date 2023/8/24 10:16
 * Description:
 */
@Slf4j
@Api(tags = "回复接口")
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;


    @ApiOperation("回复消息")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam(value = "帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam(value = "回复内容") @RequestParam("content") @NonNull String content) {
        //获取用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否禁言
        if (user.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //获取帖子信息
        Article article = articleService.selectById(articleId);
        //校验
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //帖子状态
        if (article.getState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //构建回复对象
        ArticleReply articleReply = new ArticleReply();
        //要回复的帖子Id
        articleReply.setArticalId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        //写入回复
        articleReplyService.create(articleReply);
        //返回数据
        return AppResult.success();
    }

    @ApiOperation("回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getReliesByArticleId(@ApiParam(value = "帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        //校验帖子是否存在
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service
        List<ArticleReply> replies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(replies);
    }
}
